﻿Public Class FORubahNilai

    'member variables
    Private _cSemester As CSemester
    Private _semesterAktif As tbl_semester
    Private _cMengajar As CGuruMengajar
    Private _cNilaiSiswa As CNilaiSiswa
    Private _parent As Form

    'forms
    Private WithEvents _flGuru As FLGuru
    Private WithEvents _flKelas As FLKelas
    Private WithEvents _flSemester As FLSemester

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Public Sub New(parent As Form)
        Me.New()
        _parent = parent
    End Sub


    Private Sub FORubahNilai_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'set semester aktif
        _cSemester = New CSemester
        _semesterAktif = _cSemester.getAktifSemester
        tbSemester.Text = _semesterAktif.nama
        tbSemesterValue.Text = _semesterAktif.id

        'set datagrid
        DGMaster.AutoGenerateColumns = False
    End Sub

    Private Sub tbGuru_Enter(sender As Object, e As System.EventArgs) Handles tbGuru.Enter, tbGuru.Click
        'tailkan form FLguru
        _flGuru = New FLGuru
        _flGuru.ShowDialog()
    End Sub

    Private Sub _flGuru_DataTerpilih() Handles _flGuru.DataTerpilih
        'set data guru terpilih
        tbGuru.Text = _flGuru.SelectedNamaGuru
        tbGuruValue.Text = _flGuru.SelectedIDGuru

        ''reset kelas dan mata pelajaran
        'cbKelas.DataSource = Nothing
        'cbKelas.DataSource = Nothing
        'cbMP.DataSource = Nothing
        'DGMaster.DataSource = Nothing
    End Sub

    Private Sub tbSemester_Enter(sender As Object, e As System.EventArgs) Handles tbSemester.Enter, tbSemester.Click
        _flSemester = New FLSemester
        _flSemester.ShowDialog()
    End Sub

    Private Sub _flSemester_DataTerpilih() Handles _flSemester.DataTerpilih
        'set semester terpilih 
        tbSemester.Text = _flSemester.SelectedNamaSemester
        tbSemesterValue.Text = _flSemester.SelectedIDSemester

        ''reset kelas dan mata pelajaran
        'cbKelas.DataSource = Nothing
        'cbKelasSd.DataSource = Nothing
        'cbMP.DataSource = Nothing
        'DGMaster.DataSource = Nothing
    End Sub

    Private Sub cbKelas_Click(sender As Object, e As System.EventArgs) Handles cbKelas.Click
        'If cbKelas.DataSource Is Nothing And tbSemester.Text.Trim <> "" And tbGuru.Text.Trim <> "" Then
        'menampilkan daftar kelas yang di ajar
        _cMengajar = New CGuruMengajar
        With cbKelas
            .DataSource = _cMengajar.getKelas(tbGuruValue.Text.Trim, tbSemesterValue.Text.Trim)
            .DisplayMember = "nama"
            .ValueMember = "id"
        End With
        ''set cbKelas shadow
        'With cbKelasSd
        '    .DataSource = _cMengajar.getKelas(tbGuruValue.Text.Trim, tbSemesterValue.Text.Trim)
        '    .DisplayMember = "nama"
        '    .ValueMember = "id"
        'End With

        'End If
    End Sub

    Private Sub cbMP_Click(sender As Object, e As System.EventArgs) Handles cbMP.Click
        'If cbKelas.DataSource IsNot Nothing And cbKelasSd.SelectedValue <> cbKelas.SelectedValue Then
        With cbMP
            .DataSource = _cMengajar.getMP(tbGuruValue.Text.Trim, tbSemesterValue.Text.Trim, cbKelas.SelectedValue)
            .DisplayMember = "nama"
            .ValueMember = "id"
        End With

        'End If
    End Sub

    Private Sub cbKelas_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cbKelas.SelectedIndexChanged
        'reset datagrid datasource
        DGMaster.DataSource = Nothing
    End Sub

    Private Sub cbMP_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cbMP.SelectedIndexChanged
        'set shadow selected
        If cbKelasSd.DataSource IsNot Nothing Then
            cbKelasSd.SelectedIndex = cbKelas.SelectedIndex

        End If



    End Sub

    Private Sub DGMaster_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DGMaster.RowPostPaint
        'menampilkan nomor urut pada datagridview
        DGMaster.Rows(e.RowIndex).Cells("CNo").Value = e.RowIndex + 1
    End Sub

    Private Sub cbMP_SelectionChangeCommitted(sender As Object, e As System.EventArgs) Handles cbMP.SelectionChangeCommitted
        If cbMP.DataSource IsNot Nothing Then
            'set data nilai
            _cNilaiSiswa = New CNilaiSiswa
            DGMaster.DataSource = _cNilaiSiswa.getPenilaian(tbGuruValue.Text.Trim, tbSemesterValue.Text.Trim, cbKelas.SelectedValue, cbMP.SelectedValue)
        End If
    End Sub

    Private Sub btnOk_Click(sender As System.Object, e As System.EventArgs) Handles btnOk.Click
        'tampilkan form perubahan nilai

        'set data nilai
        'Dim dTabNilai As New DataTable
        'With dTabNilai
        '    .Columns.Add("id_ssw")
        '    .Columns.Add("nama")
        '    .Columns.Add("nil")
        'End With
        'For Each arow As DataGridViewRow In DGMaster.Rows
        '    Dim dRow As DataRow = dTabNilai.NewRow
        '    dRow.Item("id_ssw") = arow.Cells("cid").Value
        '    dRow.Item("nama") = arow.Cells("cnama").Value
        '    dRow.Item("nil") = arow.Cells("cnilai").Value
        '    dTabNilai.Rows.Add(dRow)
        'Next
        _cNilaiSiswa = New CNilaiSiswa
        Dim dtabnilai As DataTable = _cNilaiSiswa.getNilaiSiswa(DGMaster.SelectedRows(0).Cells("cid").Value)

        Dim _ftpenilaian As New FTPenilaian(tbGuruValue.Text.Trim, DGMaster.SelectedRows(0).Cells("ctgl").Value, DGMaster.SelectedRows(0).Cells("CKatNil").Value, cbKelas.SelectedValue, cbMP.SelectedValue, tbSemesterValue.Text.Trim, DGMaster.SelectedRows(0).Cells("cid").Value, dtabnilai)
        With _ftpenilaian
            .MdiParent = _parent
            .Show()
        End With
        Me.Close()
    End Sub

    Private Sub btnKeluar_Click(sender As System.Object, e As System.EventArgs) Handles btnKeluar.Click
        Me.Close()
    End Sub
End Class